package com.edgar;

import com.jfinal.config.*;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

public class AppConfig extends JFinalConfig {

    public static final Prop p = PropKit.use("application.properties");

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(p.getBoolean(Contants.UNDERTOW_DEV_MODE));
        me.setMaxPostSize(1000 * 1024 * 1024);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", SystemController.class);
    }

    @Override
    public void configEngine(Engine me) {
        me.setDevMode(p.getBoolean(Contants.UNDERTOW_DEV_MODE));
        me.setToClassPathSourceFactory();
        me.setBaseTemplatePath("views");
        me.addSharedFunction("/layout.html");
        me.addSharedFunction("/embed_layout.html");
    }

    @Override
    public void configPlugin(Plugins me) {
        DruidPlugin dp = new DruidPlugin("jdbc:sqlite:" + PathKit.getWebRootPath() + "/data.sqlite", null, null);
        me.add(dp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        arp.setDialect(new Sqlite3Dialect());
        arp.setTransactionLevel(8);
        arp.setShowSql(p.getBoolean(Contants.UNDERTOW_DEV_MODE));
        arp.addMapping("t_file_zone", "zone_id", FileZone.class);
        arp.addMapping("t_file_box", "file_id", FileBox.class);
        me.add(arp);

        Cron4jPlugin cp = new Cron4jPlugin();
        cp.addTask("*/1 * * * *", new SystemTask());
        cp.addTask("*/1 * * * *", new RemoveExpireFileZoneTask());
        me.add(cp);
    }

    @Override
    public void configInterceptor(Interceptors me) {

    }

    @Override
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("ctx"));
    }

    public static void main(String[] args) {
        UndertowServer.create(AppConfig.class, "application.properties").start();
    }
}
